#include <iostream>
#include <unistd.h>
#include <uv.h>
#include <stdio.h>

#include "encapsulation/ThreadPool.h"
#include "encapsulation/ThreadAsync.h"
#include "RunnableImpl.h"

void print(uv_work_t *req) {
  sleep(1);
  long num = (long long) req->data;
  printf("thread id is: %ld, num is: %d\n", uv_thread_self(), num);
}

void after_print(uv_work_t *req, int status) {
  printf("thread id is: %ld, after print, req data is %d, status is %d\n", uv_thread_self(), req->data, status);
}

void async(ThreadAsyncData *threadAsyncData) {
  void *userData = threadAsyncData->getMainData();
  void *syncData = threadAsyncData->getSubData();
  int *num = (int *) syncData;
  std::cout << "async thread id:" << uv_thread_self() << " num:" << *num << std::endl;
}

uv_async_t *asyncData;

int main() {
  uv_loop_t *loop = uv_default_loop();
  ThreadPool threadPool(loop);
//  ThreadAsyncData data(nullptr);
//  ThreadAsync threadAsync(loop, &data);
//  threadAsync.setAsyncCB(async);
  asyncData = static_cast<uv_async_t *>(malloc(sizeof(uv_async_t)));
  for (int index = 0; index < 1; index++) {
    Runnable *runnable = new RunnableImpl(index, nullptr);
    threadPool.start(runnable);
  }
//  uv_close((uv_handle_t *) asyncData, nullptr);

  int run = uv_run(loop, UV_RUN_DEFAULT);
  uv_close((uv_handle_t *) asyncData, nullptr);
  return run;
}
